{{ $cli := site.Data.docs.cli }}

<div class="no-prose flex flex-col border dark:border-gray-700 rounded-md divide-y dark:divide-gray-700">
  <div class="py-4 px-6">
    <span class="flex justify-between items-center">
      <h3 id="{{ $cli.name }}" class="font-mono">
        {{ $cli.name }}
      </h3>
    </span>

    <div class="mt-2">
      <p>
        The root command (no subcommand) starts Vector.
      </p>

      <div class="mt-2">
        {{ highlight "vector [FLAGS] [OPTIONS] [SUBCOMMAND] [ARGS]" "shell" "" }}
      </div>

      <div class="mt-3">
        {{ template "options-table" (dict "title" "Flags" "items" $cli.flags "cmd" "vector") }}
      </div>

      <div class="mt-3">
        {{ template "options-table" (dict "title" "Options" "items" $cli.options "cmd" "vector") }}
      </div>
    </div>
  </div>


  {{ range $k, $v := $cli.commands }}
  <div class="py-4 px-6">
    <span>
      {{ partial "heading.html" (dict "text" (printf "vector %s" $k) "level" 3 "mono" true "id" $k) }}
    </span>

    <div class="mt-4">
      {{ with $v.description }}
      <div>
        {{ . | markdownify }}
      </div>
      {{ end }}

      <div class="mt-2">
        {{ template "usage" $v }}
      </div>

      {{ with $v.example }}
      <div class="mt-2">
        <span>
          {{ partial "heading.html" (dict "text" "Example" "level" 4 "toc_hide" true) }}
        </span>

        <div class="mt-2">
          {{ highlight . "shell" "" }}
        </div>
      </div>
      {{ end }}

      {{ with $v.flags }}
      <div>
        {{ template "options-table" (dict "title" "Flags" "items" . "cmd" $k) }}
      </div>
      {{ end }}

      {{ with $v.options }}
      <div>
        {{ template "options-table" (dict "title" "Options" "items" . "cmd" $k) }}
      </div>
      {{ end }}

      {{ with $v.args }}
      <div>
        {{ template "args-table" (dict "title" "Arguments" "items" . "cmd" $k) }}
      </div>
      {{ end }}
    </div>

  </div>
  {{ end }}
</div>

{{ define "usage" }}
{{ $s := printf "vector %s" .name }}
{{ if .flags }}
{{ $s = printf "%s [FLAGS]" $s }}
{{ end }}
{{ if .options }}
{{ $s = printf "%s [OPTIONS]" $s }}
{{ end }}
{{ if .args }}
{{ $s = printf "%s [ARGUMENTS]" $s }}
{{ end }}
{{ highlight $s "shell" "" }}
{{ end }}

{{ define "args-table" }}
<table>
  <thead>
    <tr>
      <th>
        {{ .title }}
      </th>
      <th>
        Required
      </th>
      <th>
        Description
      </th>
      <th>
        Type
      </th>
      <th>
        Default
      </th>
    </tr>
  </thead>
  <tbody>
    {{ range $k, $v := .items }}
    <tr>
      <td>
        <code>{{ $k }}</code>
      </td>
      <td>
        {{ $v.required }}
      </td>
      <td>
        {{ $v.description | markdownify }}
      </td>
      <td>
        {{ $v.type }}
      </td>
      <td>
        {{ with $v.default }}
        <code>{{ . }}</code>
        {{ else }}
        none
        {{ end }}
      </td>
    </tr>
    {{ end }}
  </tbody>
</table>
{{ end }}

{{ define "options-table" }}
{{ $cmd := .cmd }}
<table>
  <thead>
    <tr>
      <th>
        {{ .title }}
      </th>
      <th>
        Description
      </th>
      <th>
        Environment variable
      </th>
      <th>
        Default
      </th>
    </tr>
  </thead>

  <tbody>
    {{ range .items }}
    <tr>
      {{ if .flag }}
      {{ $id := substr .flag 2 }}
      {{ $href := printf "%s-%s" $cmd $id }}
      {{ $comma := and .flag .short }}
      <td id="{{ $href }}">
        <span class="text-xs font-mono font-semibold text-primary-dark dark:text-primary">
          {{- .flag -}}
        </span>
        {{ if $comma }},<br />{{ end }}
        {{ with .short }}
        <span class="text-xs font-mono font-semibold text-primary-dark dark:text-primary">
          {{ . }}
        </span>
        {{ end }}
      </td>
      {{ end }}

      {{ if .option }}
      {{ $id := substr .option 2 }}
      {{ $href := printf "%s-%s" $cmd $id }}
      {{ $comma := and .option .short }}
      <td id="{{ $href }}">
        <span class="text-xs font-mono font-semibold text-primary-dark dark:text-primary">
          {{- .option -}}
        </span>
        {{ if $comma }},<br />{{ end }}
        {{ with .short }}
        <span class="text-xs font-mono font-semibold text-primary-dark dark:text-primary">
          {{ . }}
        </span>
        {{ end }}
      </td>
      {{ end }}
      <td>
        <div class="prose dark:prose-dark">
          {{ .description | markdownify }}
        </div>

        {{ with .enum }}
        <div class="mt-4">
          <span class="font-semibold">
            Enum options
          </span>

          <table>
            <thead>
              <tr>
                <th>
                  Option
                </th>
                <th>
                  Description
                </th>
              </tr>
            </thead>

            <tbody>
              {{ range $k, $v := . }}
              <tr>
                <td>
                  <code>
                    {{ $k }}
                  </code>
                </td>
                <td>
                  {{ $v }}
                </td>
              </tr>
              {{ end }}
            </tbody>
          </table>
        </div>
        {{ end }}
      </td>
      <td>
        {{ with .env_var }}
        <a href="#{{ . | urlize }}">
          <code>
            {{ . }}
          </code>
        </a>
        {{ end }}
      </td>

      <td>
        <code>
          {{ .default }}
        </code>
      </td>
    </tr>
    {{ end }}
  </tbody>
</table>
{{ end }}